#include <stdio.h>
/*
int find(int *c,int left,int right,int size){
    if( left == right )
        return left;
    int mid=left+((right-left)>>1);
    int r;
    if( c[mid] >= size-mid )
        r=find(c,left,mid,size);
    else
        r=find(c,mid+1,right,size);
    return r;
}
*/


int find(int *c,int left,int right,int size){
    if( left > right )
        return left;
    int mid=left+((right-left)>>1);
    int r;
    if( c[mid] >= size-mid )
        r=find(c,left,mid-1,size);
    else
        r=find(c,mid+1,right,size);
    return r;
}

int hIndex(int* citations, int citationsSize) {
    int a=find(citations,0,citationsSize-1,citationsSize);
    printf("find :%d\n",a);
    if( (a == citationsSize-1) && (citations[citationsSize-1] == 0) ){
        return 0;
    }
    else
        return citationsSize - a ;
}

int main(){
    //int a[]={0,0,0,0,0,0,0};
    //int a[]={0,1,3,5,6};
    int a[]={5,11,12,22,22};
    //int a[]={1,2,100};
    printf("%d\n",hIndex(a,sizeof(a)/sizeof(int)));
    return 0;
}
